import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

@SuppressWarnings("serial")
public class Crossword implements Serializable {
	private static long actualID = 1;
	private LinkedList<CwEntry> entries;
	private Board b;
	private transient InteliCwDB cwdb;
	private final long ID;
	
	public Crossword(String filename)
	{
		entries = new LinkedList<CwEntry>();
		cwdb = new InteliCwDB(filename);
		b = new Board(20,20); 
		ID = actualID++;
	}
	
	public Crossword(String filename, int height, int width)
	{
		entries = new LinkedList<CwEntry>();
		cwdb = new InteliCwDB(filename);
		b = new Board(height, width); 
		ID = actualID++;
	}
	
	public Iterator<CwEntry> getROEntryIter()
	{
		return Collections.unmodifiableList(entries).iterator();
	}
	
	public Board getBoard()
	{
		return b;
	}
	
	public Board getBoardCopy()
	{
		int height = b.getHeight();
		int width = b.getWidth();
		Board copy = new Board(height, width);
		
		for(int i=0; i < height; i++)
		{
			for(int j=0; j < width; j++)
			{
				if (b.getCell(i, j) != null)
					copy.setCell(i, j, b.getCell(i, j));
			}
				
		}
		
		return copy;
	}
	
	public boolean isEmpty()
	{
		return entries.isEmpty();
	}
	
	public long getID() {
		return ID;
	}

	public InteliCwDB getCwDB()
	{
		return cwdb;
	}
	
	public void setCwDB(InteliCwDB cwdb)
	{
		this.cwdb = cwdb;
	}
	
	public boolean contains(String word)
	{
		return entries.contains(word);
	}
	
	public final void addCwEntry(CwEntry cwe, Strategy s){
		  entries.add(cwe);
		  s.updateBoard(b,cwe);
	}
	
	public final void generate(Strategy s){
		  CwEntry e = null;
		  while((e = s.findEntry(this)) != null){
		    addCwEntry(e,s);
		  }
	}
	
	public void printBoard() {
		b.print();
	}
}
